package com.marketlive.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/*
(C) Copyright MarketLive. 2006. All rights reserved.
MarketLive is a trademark of MarketLive, Inc.
Warning: This computer program is protected by copyright law and international treaties.
Unauthorized reproduction or distribution of this program, or any portion of it, may result
in severe civil and criminal penalties, and will be prosecuted to the maximum extent
possible under the law.
*/

import java.net.URLEncoder;

/**
 * Utility class for network-related functionality such as protocols, encoding, and support.
 */
public abstract class NetUtil {

  /**
   * URL-encodes a string value using UTF-8.  Turns {@link UnsupportedEncodingException} exception into a
   * RuntimeException.
   *
   * @param path a string to encode
   * @return a URL-encoded string
   * @throws RuntimeException if an UnsupportedEncodingException occurs
   */
  public static String urlEncode(String path) {
    try {
      return URLEncoder.encode(path, "UTF-8");
    } catch (UnsupportedEncodingException e) {
      throw new RuntimeException("Error encoding URL: " + path, e);
    }
  }

  /**
   * URL-decodes a string value using UTF-8.  Turns {@link UnsupportedEncodingException} into a RuntimeException.
   *
   * @param path a string to decode
   * @return a decoded string
   * @throws RuntimeException if an UnsupportedEncodingException occurs
   */
  public static String urlDecode(String path) {
    try {
      return URLDecoder.decode(path, "UTF-8");
    } catch (UnsupportedEncodingException e) {
      throw new RuntimeException("Error decoding URL: " + path, e);
    }
  }
}
